#include "kernel_operator.h"
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;    
template<typename TYPE_X, typename TYPE_Y> class KernelMishGrad {
    using T = TYPE_X;
public:
    __aicore__ inline KernelMishGrad() {}
    __aicore__ inline void Init(GM_ADDR grad, GM_ADDR x, GM_ADDR tanhx, GM_ADDR y,
                                uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum, int32_t tanhxSize) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;
        this->tanhxSize =tanhxSize;

        gradGm.SetGlobalBuffer((__gm__ DTYPE_GRAD*)grad, this->coreDataNum);
        xGm.SetGlobalBuffer((__gm__ DTYPE_X*)x, this->coreDataNum);
        tanhxGm.SetGlobalBuffer((__gm__ DTYPE_TANHX*)tanhx, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);

        pipe.InitBuffer(inQueueGrad, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_GRAD));
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X));
        pipe.InitBuffer(inQueueTanhx, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_TANHX));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));

        pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(DTYPE_X));
        pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(DTYPE_X));
    }
    __aicore__ inline void Process() {

        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        LocalTensor<DTYPE_GRAD> gradLocal = inQueueGrad.AllocTensor<DTYPE_GRAD>();
        LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();
        LocalTensor<DTYPE_TANHX> tanhxLocal = inQueueTanhx.AllocTensor<DTYPE_TANHX>();
        DataCopy(gradLocal, gradGm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
        if(this->tanhxSize != 0)
            DataCopy(tanhxLocal, tanhxGm[progress * this->tileDataNum], this->processDataNum);
        inQueueGrad.EnQue(gradLocal);
        inQueueX.EnQue(xLocal);
        inQueueTanhx.EnQue(tanhxLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        LocalTensor<DTYPE_GRAD> gradLocal = inQueueGrad.DeQue<DTYPE_GRAD>();
        LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();
        LocalTensor<DTYPE_X> tanhxLocal = inQueueTanhx.DeQue<DTYPE_X>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();
        auto tmp1 = QueueTmp1.Get<DTYPE_X>();
        auto tmp2 = QueueTmp2.Get<DTYPE_X>();

        if(this->tanhxSize != 0)
        {
            Mul(yLocal, tanhxLocal, tanhxLocal, this->processDataNum);

            Muls(yLocal, yLocal, static_cast<DTYPE_X>(-1), this->processDataNum);
            Adds(yLocal, yLocal, static_cast<DTYPE_X>(1), this->processDataNum);

            Mul(yLocal, yLocal, xLocal, this->processDataNum);


            Exp(tmp1, xLocal, this->processDataNum);
            Mul(yLocal, yLocal, tmp1, this->processDataNum);

            Adds(tmp2, tmp1, static_cast<DTYPE_X>(1), this->processDataNum);

            Div(yLocal, yLocal, tmp2, this->processDataNum);

            Add(yLocal, yLocal, tanhxLocal, this->processDataNum);

            Mul(yLocal, yLocal, gradLocal, this->processDataNum);
        }
        else
        {
            Exp(tmp1, xLocal, this->processDataNum);

            Adds(tmp2, tmp1, static_cast<DTYPE_X>(1), this->processDataNum);

            Mul(yLocal, tmp2, tmp2, this->processDataNum);
            Adds(yLocal, yLocal, static_cast<DTYPE_X>(1), this->processDataNum);
            
            Mul(tmp2, tmp2, tmp1, this->processDataNum);
            Mul(tmp2, tmp2, xLocal, this->processDataNum);

            Div(tmp2, tmp2, yLocal, this->processDataNum);
            Div(tmp2, tmp2, yLocal, this->processDataNum);

            Muls(tmp2, tmp2, static_cast<DTYPE_X>(4), this->processDataNum);

            Muls(yLocal, yLocal, static_cast<DTYPE_X>(2), this->processDataNum);

            Sub(tmp2, tmp2, yLocal, this->processDataNum);

            Adds(yLocal, tmp2, static_cast<DTYPE_X>(1), this->processDataNum);

            Mul(yLocal, yLocal, gradLocal, this->processDataNum);

        }

        outQueueY.EnQue<TYPE_Y>(yLocal);
        inQueueGrad.FreeTensor(gradLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueTanhx.FreeTensor(tanhxLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueGrad, inQueueX, inQueueTanhx;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmp2;

    GlobalTensor<DTYPE_GRAD> gradGm;
    GlobalTensor<DTYPE_X> xGm;
    GlobalTensor<DTYPE_TANHX> tanhxGm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;

    int32_t tanhxSize;
};
extern "C" __global__ __aicore__ void mish_grad(GM_ADDR grad, GM_ADDR x, GM_ADDR tanhx, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    KernelMishGrad<DTYPE_X, DTYPE_Y> op;
    op.Init(grad, x, tanhx, y, 
            tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum, tiling_data.tanhxSize);  
    op.Process();
}